package com.example.musicaiap.ui.components

import androidx.compose.animation.animateColorAsState
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.MusicNote
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ModalBottomSheet
import androidx.compose.material3.SheetState
import androidx.compose.material3.Text
import androidx.compose.material3.rememberModalBottomSheetState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.media3.common.MediaItem
import coil.compose.AsyncImage
import coil.request.ImageRequest
import com.example.musicaiap.domain.model.Song
import com.example.musicaiap.ui.theme.QQGreen

/**
 * 当前播放列表底部弹窗
 * 
 * 显示当前播放队列中的所有歌曲，并标记当前正在播放的歌曲
 * 
 * @param isVisible 控制底部弹窗的显示/隐藏
 * @param onDismiss 弹窗关闭回调
 * @param playQueue 播放队列中的歌曲
 * @param currentSong 当前播放的歌曲
 * @param onSongClick 歌曲点击回调
 */
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun PlaylistBottomSheet(
    isVisible: Boolean,
    onDismiss: () -> Unit,
    playQueue: List<Song>,
    currentSong: Song?,
    onSongClick: (Song) -> Unit
) {
    if (!isVisible) return
    
    val sheetState = rememberModalBottomSheetState()
    
    ModalBottomSheet(
        onDismissRequest = onDismiss,
        sheetState = sheetState,
        containerColor = MaterialTheme.colorScheme.surface,
        contentColor = MaterialTheme.colorScheme.onSurface
    ) {
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .padding(bottom = 32.dp)
        ) {
            // 标题
            Text(
                text = "当前播放列表",
                style = MaterialTheme.typography.titleMedium,
                modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp)
            )
            
            // 分隔线
            Spacer(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(1.dp)
                    .background(MaterialTheme.colorScheme.onSurface.copy(alpha = 0.1f))
                    .padding(vertical = 8.dp)
            )
            
            // 播放队列
            if (playQueue.isNotEmpty()) {
                SongList(
                    songs = playQueue,
                    currentSong = currentSong,
                    onSongClick = onSongClick
                )
            } else {
                // 空播放队列
                Box(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(32.dp),
                    contentAlignment = Alignment.Center
                ) {
                    Text(
                        text = "播放列表为空",
                        style = MaterialTheme.typography.bodyMedium,
                        color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.6f)
                    )
                }
            }
        }
    }
}

/**
 * 歌曲列表
 */
@Composable
private fun SongList(
    songs: List<Song>,
    currentSong: Song?,
    onSongClick: (Song) -> Unit
) {
    LazyColumn {
        items(songs) { song ->
            SongItem(
                song = song,
                isPlaying = currentSong?.id == song.id,
                onSongClick = onSongClick
            )
        }
    }
}

/**
 * 歌曲列表项
 */
@Composable
private fun SongItem(
    song: Song,
    isPlaying: Boolean,
    onSongClick: (Song) -> Unit
) {
    // 当前播放的歌曲使用动画颜色变化
    val textColor by animateColorAsState(
        targetValue = if (isPlaying) QQGreen else MaterialTheme.colorScheme.onSurface,
        label = "textColor"
    )
    
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .clickable { onSongClick(song) }
            .padding(horizontal = 16.dp, vertical = 8.dp),
        verticalAlignment = Alignment.CenterVertically
    ) {
        // 封面图片
        if (song.coverUrl.isNotEmpty() && (song.coverUrl.startsWith("http") || song.coverUrl.startsWith("content://"))) {
            AsyncImage(
                model = ImageRequest.Builder(LocalContext.current)
                    .data(song.coverUrl)
                    .crossfade(true)
                    .build(),
                contentDescription = "专辑封面",
                contentScale = ContentScale.Crop,
                modifier = Modifier
                    .size(40.dp)
                    .clip(RoundedCornerShape(4.dp))
            )
        } else {
            // 默认音乐图标
            Box(
                modifier = Modifier
                    .size(40.dp)
                    .clip(RoundedCornerShape(4.dp))
                    .background(MaterialTheme.colorScheme.surfaceVariant),
                contentAlignment = Alignment.Center
            ) {
                Icon(
                    imageVector = Icons.Filled.MusicNote,
                    contentDescription = null,
                    tint = MaterialTheme.colorScheme.onSurfaceVariant
                )
            }
        }
        
        Spacer(modifier = Modifier.width(12.dp))
        
        // 歌曲信息
        Column(
            modifier = Modifier.weight(1f)
        ) {
            Text(
                text = song.title,
                style = MaterialTheme.typography.bodyMedium,
                maxLines = 1,
                overflow = TextOverflow.Ellipsis,
                color = textColor,
                fontWeight = if (isPlaying) FontWeight.Bold else FontWeight.Normal
            )
            
            Text(
                text = song.artist,
                style = MaterialTheme.typography.bodySmall,
                fontSize = 12.sp,
                maxLines = 1,
                overflow = TextOverflow.Ellipsis,
                color = if (isPlaying) textColor.copy(alpha = 0.7f) else MaterialTheme.colorScheme.onSurface.copy(alpha = 0.7f)
            )
        }
    }
} 